/*
 * Villager.cpp
 *
 *  Created on: 25.1.2013
 *      Author: Travis
 */
#ifdef _WIN32 
#include <windows.h> 
#endif

#include "Villager.h"
#include "../../static/building/Building.h"

Villager::Villager(glm::vec3 _Pos, float _r, float _speed, std::string _Name,
		std::string _modelFName, ModelPool *_mPool, PathFinder *_pathfinder,
		Terrain::Terrain *_ter) :
		dynamicEntity(_Pos, _r, _speed, _Name, _modelFName, _mPool, _pathfinder,
				_ter)
{
	type = OBJ_VILLAGER;
	parentType = OBJ_DYNAMIC;

	// TEST loading animation

	//modelID.clear();

	for (int i = 0; i < 33; i++)
	{
		std::stringstream ss; //create a stringstream
		ss << i + 1; //add number to the stream
		modelID.push_back(mPool->loadModel(modelFName, "_a_00000" + ss.str()));
	}
	//----------------------------

	NewTarget = false;
	tar = glm::vec2(0.0);

	costGold = 50;

	owner = NULL;
}

Villager::~Villager()
{
}

void Villager::goTo(glm::vec2 pos)
{
	tar = pos;
	NewTarget = true;
	resFlag = true;
	newMove = true;
}

Entity* Villager::getOwner()
{
	return owner;
}

void Villager::setOwner(Entity *entity)
{
	this->owner = entity;
}

void Villager::setWork()
{
	status = STATUS_NEWJOB;
}

void Villager::doWork()
{
	status = STATUS_DONE;
}

void Villager::goHome()
{
	if (owner->getParentType() == OBJ_BUILDING)
	{
		Building *building = static_cast<Building*>(owner);

		if (building != NULL)
		{
			goTo(building->get2dPosition());

			status = STATUS_GOHOME;
		}
		else
			status = STATUS_READY;
	}
	else
		status = STATUS_READY;
}

void Villager::goWork()
{
	status = STATUS_GOWORK;
}

void Villager::goingWork()
{
	// special overriden in children
}

void Villager::goingHome()
{
	// special overriden in children
}

void Villager::backHome()
{
	// special overriden in children
	status = STATUS_READY;
}

void Villager::AIFunction()
{
	if (status == STATUS_SLEEPING)
		return;

	if (status == STATUS_NEWJOB)
		goWork();
	else if (status == STATUS_GOWORK)
		goingWork();
	else if (status == STATUS_WORKING)
		// do work when status is working
		doWork();
	else if (status == STATUS_DONE)
		// when job is done go back home
		goHome();
	else if (status == STATUS_GOHOME)
		goingHome();
	else if (status == STATUS_BACKHOME)
		backHome();
}

void Villager::control(float t)
{
	if (sleeping)
		return;

	// call AI function
	AIFunction();

	if (NewTarget == true)
	{
		MoveVector = pathfinder->getPath(tar, get2dPosition());
		resFlag = true;
		newMove = true;
		NewTarget = false;
	}

	if (MoveVector.size() > 0)
	{
		Moving = true;
		move();
	}
	else
	{
		Moving = false;

		// when entity is in final destination chck status
		// when already finished move was to do something -> change status to start work
		// else entity is home set its status to ready
		if (status == STATUS_GOWORK)
			status = STATUS_WORKING;
		else if (status == STATUS_GOHOME)
			status = STATUS_BACKHOME;
	}

	updateAnimation();
	visible = World::world->SphereInFrustum(Pos, 1.0);
}

